import gdepot.Category
import gdepot.Goods
import gdepot.Bank
import gdepot.Role
import gdepot.User
import gdepot.UserRole

class BootStrap {

    def init = { servletContext ->

        if(User.count() < 1){
            def user = new User()
            user.username = 'admin'
            user.password = 'admin'
            user.fullName = 'admin'
            user.phone = '18335768850'
            user.email = '1556913110@qq.com'
            user.save flush: true
        }
        if(Role.count() < 1){
            def role = new Role()
            role.authority = "ROLE_SYSTEM_MANAGER"
            role.save flush: true
        }

        if(UserRole.count() < 1){
           UserRole.create(User.findByUsername('admin'),Role.findByAuthority('ROLE_SYSTEM_MANAGER'),true)

        }

        if(Category.count() <1)
        {
            def category=new Category()
            category.categoryName = "计算机"
            category.user = User.findByUsername("admin")
            category.save flush: true
            println 'new category saves()!'
        }



       if (Goods.count()<1){

           def good = new Goods()
           good.category = Category.findByCategoryName("计算机")
           good.author  = "张三"
           good.photoUrl = ""
           good.description = "一本值阅读的好书"
           good.price = 12
           good.title = "Java"
           good.publish = "清华大学出版社"
           good.user = User.findByUsername("admin")
           good.save flush: true
            println ' good saved!'
        }


        // 用于验卡和支付
        if (Bank.count() < 1)
        {
            println 'init Bank'
            // 1 快捷支付
            def bank = new Bank()
            bank.code = '0102'
            bank.name = '中国工商银行'
            bank.singleLimit = 50000.00
            bank.dailyLimit = 100000.00
            bank.monthlyLimit = 999999999999999.99// 表示无限制
            bank.imageName = 'icbc.bmp'
            bank.save flush:true
            // 2
            bank = new Bank()
            bank.code = '0103'
            bank.name = '中国农业银行'
            bank.singleLimit = 20000.00
            bank.dailyLimit = 20000.00
            bank.monthlyLimit = 999999999999999.99
            bank.imageName = 'abc.bmp'
            bank.save flush:true
            // 3
            bank = new Bank()
            bank.code = '0105'
            bank.name = '中国建设银行'
            bank.singleLimit = 500000.00
            bank.dailyLimit = 500000.00
            bank.monthlyLimit = 999999999999999.99
            bank.imageName = 'ccb.bmp'
            bank.save flush:true
            // 4
            bank = new Bank()
            bank.code = '0104'
            bank.name = '中国银行'
            bank.singleLimit = 50000.00
            bank.dailyLimit =  200000.00
            bank.monthlyLimit = 999999999999999.99
            bank.imageName = 'bc.bmp'
            bank.save flush:true
            // 5
            bank = new Bank()
            bank.code = '0309'
            bank.name = '兴业银行'
            bank.singleLimit = 50000.00
            bank.dailyLimit = 50000.00
            bank.imageName = 'cib.bmp'
            bank.save flush:true
            // 6
            bank = new Bank()
            bank.code = '0303'
            bank.name = '中国光大银行'
            bank.singleLimit = 500000.00
            bank.dailyLimit = 500000.00
            bank.imageName = 'guangda.bmp'
            bank.save flush:true
            // 7
            bank = new Bank()
            bank.code = '0302'
            bank.name = '中信银行'
            bank.singleLimit = 50000.00
            bank.dailyLimit = 500000.00
//            bank.imageName = '8891-B.gif'
            bank.imageName='zx.bmp'
            bank.save flush:true
            // 8
            bank = new Bank()
            bank.code = '0310'
            bank.name = '上海浦东发展银行'
            bank.singleLimit = 50000.00
            bank.dailyLimit = 500000.00
//            bank.imageName = '8892-B.gif'
            bank.imageName='shpd.bmp'
            bank.save flush:true
            // 10
            bank = new Bank()
            bank.code = '0403'
            bank.name = '中国邮政储蓄银行'
            bank.singleLimit = 500000.00
            bank.dailyLimit = 500000.00
            bank.monthlyLimit = 999999999999999.99
            bank.imageName = 'post.bmp'
            bank.save flush:true
            // 11
            bank = new Bank()
            bank.code = '0307'
            bank.name = '平安银行'
            bank.singleLimit = 500000.00
            bank.dailyLimit = 500000.00
            bank.monthlyLimit = 999999999999999.99
            bank.imageName = 'pingan.bmp'
            bank.save flush:true

            // 14
            bank = new Bank()
            bank.code = '0308'
            bank.name = '招商银行'
            bank.singleLimit = 1000.00
            bank.dailyLimit = 500000.00
            bank.monthlyLimit = 999999999999999.99
//            bank.imageName = '0308-B.gif'
            bank.imageName='cmb.bmp'
            bank.save flush:true
            // 15
            bank = new Bank()
            bank.code = '0306'
            bank.name = '广东发展银行'
            bank.singleLimit = 500000.00
            bank.dailyLimit = 500000.00
            bank.monthlyLimit = 999999999999999.99
//            bank.imageName = '8885-B.gif'
            bank.imageName='gf.bmp'
            bank.save flush:true
            // 16
            bank = new Bank()
            bank.code = '0305'
            bank.name = '中国民生银行'
            bank.singleLimit = 500000.00
            bank.dailyLimit = 500000.00
            bank.monthlyLimit = 999999999999999.99
            bank.imageName = 'cmbc.bmp'
            bank.save flush:true
            // 17
            bank = new Bank()
            bank.code = '0304'
            bank.name = '华夏银行'
            bank.singleLimit = 500000.00
            bank.dailyLimit = 500000.00
            bank.monthlyLimit = 999999999999999.99
            bank.imageName = 'hx.bmp'
            bank.save flush:true
            println "init bank"
        }

    }
    def destroy = {
    }
}
